This is an R Markdown Notebook. When you execute code within the notebook, the results appear beneath the code.

Try executing this chunk by clicking the Run button within the chunk or by placing your cursor inside it and pressing Ctrl+Shift+Enter.

###############  Load R6 Class DEPENDENCIES ############################
## Biparite Environment Search Simulation Class
SaomNkRSienaBiEnv <- source(file.path(dir_r, 'SAOM_NK_R6_model.R'))$value
# ## RSiena search Class
# SaomNkRSienaBiEnv_search_rsiena <- source(file.path(dir_proj, 'SAOM_NK_R6_search_rsiena_model.R'))$value
## default settings: Users do not change; TODO: implment within restricted class attributes
DV_NAME <- 'self$bipartite_rsienaDV'

1. NO Endogenous Noise in Environment (Structural Effects = 0)

1.1 Environment Configuration

#
environ_params <- list(
  M = 12,       ## Actors
  N = 6,       ## Components
  BI_PROB = 0, ## Environmental Density (DGP hyperparameter)
  component_matrix_start = 'rand', ##**TODO** Implement: 'rand','modular','semi-modular',...
  rand_seed = 1234,
  plot_init = F,
  name = '_test_tutorial_nb_'
)
#
env1 <- SaomNkRSienaBiEnv$new(environ_params)
## 
## TEST FROM CALLED CLASS INIT *BEFORE* BASE INIT
## 
## CALLED _BASE_ INIT
## 
## TEST FROM CALLED CLASS INIT *AFTER* BASE INIT

1.2 Structrual Model

SAOM Objective Function serves as the stochastic actor’s utility function for strategic search.

#
strategies <- list(
  egoX   =   c(-1, 0, 1),
  inPopX =   c( 1, 0, -1)
)

## 2.b. Component Payoffs vector
set.seed(12345)
component_payoffs <-  runif(environ_params$N, min = 0, max = 1)
## 2. Strategies sets the objective function as a linear combination of network stats across DVs
#
actor_strats_list <- lapply(strategies, function(strat) rep(strat,  environ_params$M/length(strat)) )
#
structure_model <- list(
  dv_bipartite = list(
    name = 'self$bipartite_rsienaDV',
    effects = list( ##**STRUCTURAL EFFECTS -- dyadic/network endogeneity sources**
      list(effect='density', parameter= 0, fix=T, dv_name=DV_NAME), ##interaction1 = NULL
      list(effect='inPop',   parameter= 0, fix=T, dv_name=DV_NAME), #interaction1 = NUL
      list(effect='outAct',  parameter= 0, fix=T, dv_name=DV_NAME)
    ),
    ## COVARIATE EFFECTS
    coCovars = list( 
      ##** COMPONENTS : MONADIC CONSTANT COVARIATE EFFECTS **##
      list(effect='altX',   parameter= 1, dv_name=DV_NAME, fix=T,
           interaction1='self$component_1_coCovar', x = component_payoffs 
      ),
      ##** STRATEGIES : MONADIC CONSTANT COVARIATE EFFECTS **##
      list(effect='egoX',   parameter= .1,  dv_name=DV_NAME, fix=T,
           interaction1='self$strat_1_coCovar',   x = actor_strats_list[[1]] 
      ), #interaction1 = NULL
      list(effect='inPopX', parameter= .1,  dv_name=DV_NAME, fix=T,
           interaction1='self$strat_2_coCovar',  x = actor_strats_list[[2]] 
      )
    ),
    varCovars = list() ##**MONADIC TIME-VARYING COVARIATE EFFECTS -- DYNAMIC STRATEGY PROGRAMS**
  )
)
env1$preview_effects(structure_model, filter=FALSE)
## Effects documentation written to file C:/Users/sdr8y/OneDrive - University of Missouri/Research/Search_networks/SaoMNK/R/_rsiena_effects_doc_.html.html .
env1$preview_effects(structure_model, filter=TRUE)
## Effects documentation written to file C:/Users/sdr8y/OneDrive - University of Missouri/Research/Search_networks/SaoMNK/R/_rsiena_effects_doc_.html.html .

1.3 Run RSiena Search Process

## Run Rsiena search using variable parameters in theta_matrix
env1$search_rsiena(
  structure_model,
  iterations = env1$M * 4,
  digits = 4,
  run_seed = 12345
)
## 
## 
##  theta_matrix : 
## 
##       density inPop outAct altX egoX inPopX
##  [1,]       0     0      0    1  0.1    0.1
##  [2,]       0     0      0    1  0.1    0.1
##  [3,]       0     0      0    1  0.1    0.1
##  [4,]       0     0      0    1  0.1    0.1
##  [5,]       0     0      0    1  0.1    0.1
##  [6,]       0     0      0    1  0.1    0.1
##  [7,]       0     0      0    1  0.1    0.1
##  [8,]       0     0      0    1  0.1    0.1
##  [9,]       0     0      0    1  0.1    0.1
## [10,]       0     0      0    1  0.1    0.1
## [11,]       0     0      0    1  0.1    0.1
## [12,]       0     0      0    1  0.1    0.1
## [13,]       0     0      0    1  0.1    0.1
## [14,]       0     0      0    1  0.1    0.1
## [15,]       0     0      0    1  0.1    0.1
## [16,]       0     0      0    1  0.1    0.1
## [17,]       0     0      0    1  0.1    0.1
## [18,]       0     0      0    1  0.1    0.1
## [19,]       0     0      0    1  0.1    0.1
## [20,]       0     0      0    1  0.1    0.1
## [21,]       0     0      0    1  0.1    0.1
## [22,]       0     0      0    1  0.1    0.1
## [23,]       0     0      0    1  0.1    0.1
## [24,]       0     0      0    1  0.1    0.1
## [25,]       0     0      0    1  0.1    0.1
## [26,]       0     0      0    1  0.1    0.1
## [27,]       0     0      0    1  0.1    0.1
## [28,]       0     0      0    1  0.1    0.1
## [29,]       0     0      0    1  0.1    0.1
## [30,]       0     0      0    1  0.1    0.1
## [31,]       0     0      0    1  0.1    0.1
## [32,]       0     0      0    1  0.1    0.1
## [33,]       0     0      0    1  0.1    0.1
## [34,]       0     0      0    1  0.1    0.1
## [35,]       0     0      0    1  0.1    0.1
## [36,]       0     0      0    1  0.1    0.1
## [37,]       0     0      0    1  0.1    0.1
## [38,]       0     0      0    1  0.1    0.1
## [39,]       0     0      0    1  0.1    0.1
## [40,]       0     0      0    1  0.1    0.1
## [41,]       0     0      0    1  0.1    0.1
## [42,]       0     0      0    1  0.1    0.1
## [43,]       0     0      0    1  0.1    0.1
## [44,]       0     0      0    1  0.1    0.1
## [45,]       0     0      0    1  0.1    0.1
## [46,]       0     0      0    1  0.1    0.1
## [47,]       0     0      0    1  0.1    0.1
## [48,]       0     0      0    1  0.1    0.1
## 
## 
## self$rsiena_data : 
## 
## Dependent variables:  self$bipartite_rsienaDV 
## Number of observations: 2 
## 
## Nodesets                 ACTORS      COMPONENTS 
## Number of nodes              12               6 
## 
## Dependent variable self$bipartite_rsienaDV
## Type               bipartite              
## Observations       2                      
## First nodeset      ACTORS                 
## Second nodeset     COMPONENTS             
## Densities          NA NA                  
## 
## Constant covariates:  self$component_1_coCovar, self$strat_1_coCovar, self$strat_2_coCovar 
## 
##  structural effects i=1, j=1
## $effect
## [1] "density"
## 
## $parameter
## [1] 0
## 
## $fix
## [1] TRUE
## 
## $dv_name
## [1] "self$bipartite_rsienaDV"
## 
##   effectName          include fix  test  initialValue parm
## 1 outdegree (density) TRUE    TRUE FALSE   -1.60944   0   
##   effectName          include fix  test  initialValue parm
## 1 outdegree (density) TRUE    TRUE FALSE          0   0   
## 
##  structural effects i=1, j=2
## $effect
## [1] "inPop"
## 
## $parameter
## [1] 0
## 
## $fix
## [1] TRUE
## 
## $dv_name
## [1] "self$bipartite_rsienaDV"
## 
##   effectName            include fix  test  initialValue parm
## 1 indegree - popularity TRUE    TRUE FALSE          0   0   
##   effectName            include fix  test  initialValue parm
## 1 indegree - popularity TRUE    TRUE FALSE          0   0   
## 
##  structural effects i=1, j=3
## $effect
## [1] "outAct"
## 
## $parameter
## [1] 0
## 
## $fix
## [1] TRUE
## 
## $dv_name
## [1] "self$bipartite_rsienaDV"
## 
##   effectName           include fix  test  initialValue parm
## 1 outdegree - activity TRUE    TRUE FALSE          0   0   
##   effectName           include fix  test  initialValue parm
## 1 outdegree - activity TRUE    TRUE FALSE          0   0   
## 
##  coCovars i=1, j=1
## $effect
## [1] "altX"
## 
## $parameter
## [1] 1
## 
## $dv_name
## [1] "self$bipartite_rsienaDV"
## 
## $fix
## [1] TRUE
## 
## $interaction1
## [1] "self$component_1_coCovar"
## 
## $x
## [1] 0.7209039 0.8757732 0.7609823 0.8861246 0.4564810 0.1663718
## 
##   effectName                     include fix  test  initialValue parm
## 1 self$component_1_coCovar alter TRUE    TRUE FALSE          0   0   
##   effectName                     include fix  test  initialValue parm
## 1 self$component_1_coCovar alter TRUE    TRUE FALSE          0   1   
## 
##  coCovars i=1, j=2
## $effect
## [1] "egoX"
## 
## $parameter
## [1] 0.1
## 
## $dv_name
## [1] "self$bipartite_rsienaDV"
## 
## $fix
## [1] TRUE
## 
## $interaction1
## [1] "self$strat_1_coCovar"
## 
## $x
##  [1] -1  0  1 -1  0  1 -1  0  1 -1  0  1
## 
##   effectName               include fix  test  initialValue parm
## 1 self$strat_1_coCovar ego TRUE    TRUE FALSE          0   0   
##   effectName               include fix  test  initialValue parm
## 1 self$strat_1_coCovar ego TRUE    TRUE FALSE          0   0.1 
## 
##  coCovars i=1, j=3
## $effect
## [1] "inPopX"
## 
## $parameter
## [1] 0.1
## 
## $dv_name
## [1] "self$bipartite_rsienaDV"
## 
## $fix
## [1] TRUE
## 
## $interaction1
## [1] "self$strat_2_coCovar"
## 
## $x
##  [1]  1  0 -1  1  0 -1  1  0 -1  1  0 -1
## 
##   effectName                                    include fix  test  initialValue
## 1 ind. pop.^(1/#) weighted self$strat_2_coCovar TRUE    TRUE FALSE          0  
##   parm
## 1 1   
##   effectName                                    include fix  test  initialValue
## 1 ind. pop.^(1/#) weighted self$strat_2_coCovar TRUE    TRUE FALSE          0  
##   parm
## 1 0.1 
## If you use this algorithm object, siena07 will create/use an output file C:/Users/sdr8y/OneDrive - University of Missouri/Research/Search_networks/SaoMNK/R/_test_tutorial_nb__173813296720.txt .
## 
## Start phase 0 
## theta: 0 0 0 0 0 0 
## 
## Start phase 3 
## Parameter values used for simulations
## 
##                                                           Mean      Standard      
##                                                             value   Deviation     
## 
## Rate parameters: 
##   0       Rate parameter                                   NA     ( NA        )   
## 
## Other parameters: 
##   1. eval outdegree (density)                             0.0     (  0        )   
##   2. eval indegree - popularity                           0.0     (  0        )   
##   3. eval outdegree - activity                            0.0     (  0        )   
##   4. eval self$component_1_coCovar alter                  1.0     (  0        )   
##   5. eval self$strat_1_coCovar ego                        0.1     (  0        )   
##   6. eval ind. pop.^(1/0.1) weighted self$strat_2_coCovar 0.1     (  0        )   
## 
## Simulated means and standard deviations
##   1. Number of ties                                             0.792    0.410 
##   2. Sum of squared indegrees                                   0.792    0.410 
##   3. Sum of squared outdegrees                                  0.792    0.410 
##   4. Sum of indegrees x self$component_1_coCovar                0.019    0.208 
##   5. Sum of outdegrees x self$strat_1_coCovar                  -0.042    0.798 
##   6. indegree pop.^(1/0.1) weighted self$strat_2_coCovar        0.042    0.798 
## 
## 
## Simulated statistics are in x$sf
## and simulated dependent variables in x$sims, where x is the created object.
## 
## Total of 48 iteration steps.
## 
## Covariance matrix of estimates (correlations below diagonal)
## 
##            0            0            0            0            0            0
##          NaN            0            0            0            0            0
##          NaN          NaN            0            0            0            0
##          NaN          NaN          NaN            0            0            0
##          NaN          NaN          NaN          NaN            0            0
##          NaN          NaN          NaN          NaN          NaN            0
## 
## Derivative matrix of expected statistics X by parameters:
## 
##        0.165        0.165        0.165        0.004       -0.016        0.016
##        0.165        0.165        0.165        0.004       -0.016        0.016
##        0.165        0.165        0.165        0.004       -0.016        0.016
##        0.004        0.004        0.004        0.042       -0.022        0.022
##       -0.002       -0.002       -0.002       -0.003        0.087       -0.087
##        0.000        0.000        0.000        0.000        0.000        0.000
## 
## Covariance matrix of X (correlations below diagonal):
## 
##        0.168        0.168        0.168        0.004       -0.009        0.009
##        1.000        0.168        0.168        0.004       -0.009        0.009
##        1.000        1.000        0.168        0.004       -0.009        0.009
##        0.048        0.048        0.048        0.043       -0.022        0.022
##       -0.027       -0.027       -0.027       -0.131        0.637       -0.637
##        0.027        0.027        0.027        0.131       -1.000        0.637
## 
## 
## 
## Simulated Decision Chain Summary:
## 
##    dv_type           dv_type_bin  dv_varname           id_from      
##  Length:48          Min.   :0    Length:48          Min.   : 1.000  
##  Class :character   1st Qu.:0    Class :character   1st Qu.: 4.000  
##  Mode  :character   Median :0    Mode  :character   Median : 7.000  
##                     Mean   :0                       Mean   : 6.708  
##                     3rd Qu.:0                       3rd Qu.: 9.250  
##                     Max.   :0                       Max.   :12.000  
##      id_to       beh_difference reciprocal_rate   LogOptionSetProb
##  Min.   :1.000   Min.   :0      Min.   :0.08333   Min.   :-2.485  
##  1st Qu.:2.000   1st Qu.:0      1st Qu.:0.08333   1st Qu.:-2.485  
##  Median :4.000   Median :0      Median :0.08333   Median :-2.485  
##  Mean   :4.146   Mean   :0      Mean   :0.08333   Mean   :-2.485  
##  3rd Qu.:6.000   3rd Qu.:0      3rd Qu.:0.08333   3rd Qu.:-2.485  
##  Max.   :7.000   Max.   :0      Max.   :0.08333   Max.   :-2.485  
##  LogChoiceProb      diagonal         stability       tie_change     
##  Min.   :-2.465   Length:48          Mode :logical   Mode :logical  
##  1st Qu.:-2.081   Class :character   FALSE:38        FALSE:10       
##  Median :-1.887   Mode  :character   TRUE :10        TRUE :38       
##  Mean   :-1.954                                                     
##  3rd Qu.:-1.755                                                     
##  Max.   :-1.717                                                     
##  chain_step_id  
##  Min.   : 1.00  
##  1st Qu.:12.00  
##  Median :24.00  
##  Mean   :24.27  
##  3rd Qu.:36.25  
##  Max.   :48.00  
## [1] 48 13
##    dv_type dv_type_bin              dv_varname id_from id_to beh_difference
## 1  Network           0 self$bipartite_rsienaDV      11     5              0
## 2  Network           0 self$bipartite_rsienaDV       4     4              0
## 3  Network           0 self$bipartite_rsienaDV      12     1              0
## 4  Network           0 self$bipartite_rsienaDV       9     1              0
## 5  Network           0 self$bipartite_rsienaDV       6     3              0
## 6  Network           0 self$bipartite_rsienaDV       3     7              0
## 7  Network           0 self$bipartite_rsienaDV       9     4              0
## 8  Network           0 self$bipartite_rsienaDV       9     4              0
## 9  Network           0 self$bipartite_rsienaDV       1     2              0
## 10 Network           0 self$bipartite_rsienaDV       5     3              0
## 11 Network           0 self$bipartite_rsienaDV       2     6              0
## 12 Network           0 self$bipartite_rsienaDV       4     3              0
## 13 Network           0 self$bipartite_rsienaDV       8     7              0
## 14 Network           0 self$bipartite_rsienaDV       4     2              0
## 15 Network           0 self$bipartite_rsienaDV       6     5              0
## 16 Network           0 self$bipartite_rsienaDV       6     2              0
## 17 Network           0 self$bipartite_rsienaDV       4     7              0
## 18 Network           0 self$bipartite_rsienaDV      12     2              0
## 19 Network           0 self$bipartite_rsienaDV      12     5              0
## 20 Network           0 self$bipartite_rsienaDV       5     3              0
## 21 Network           0 self$bipartite_rsienaDV       8     7              0
## 22 Network           0 self$bipartite_rsienaDV       7     2              0
## 23 Network           0 self$bipartite_rsienaDV       1     1              0
## 24 Network           0 self$bipartite_rsienaDV      10     4              0
## 25 Network           0 self$bipartite_rsienaDV       1     7              0
## 26 Network           0 self$bipartite_rsienaDV       1     4              0
## 27 Network           0 self$bipartite_rsienaDV       7     5              0
## 28 Network           0 self$bipartite_rsienaDV       2     3              0
## 29 Network           0 self$bipartite_rsienaDV       8     7              0
## 30 Network           0 self$bipartite_rsienaDV       7     7              0
## 31 Network           0 self$bipartite_rsienaDV      11     2              0
## 32 Network           0 self$bipartite_rsienaDV      10     3              0
## 33 Network           0 self$bipartite_rsienaDV      11     7              0
## 34 Network           0 self$bipartite_rsienaDV       1     2              0
## 35 Network           0 self$bipartite_rsienaDV       7     1              0
## 36 Network           0 self$bipartite_rsienaDV       9     6              0
## 37 Network           0 self$bipartite_rsienaDV      10     1              0
## 38 Network           0 self$bipartite_rsienaDV      10     5              0
## 39 Network           0 self$bipartite_rsienaDV       3     1              0
## 40 Network           0 self$bipartite_rsienaDV       7     5              0
## 41 Network           0 self$bipartite_rsienaDV      12     7              0
## 42 Network           0 self$bipartite_rsienaDV       7     6              0
## 43 Network           0 self$bipartite_rsienaDV       3     5              0
## 44 Network           0 self$bipartite_rsienaDV       6     6              0
## 45 Network           0 self$bipartite_rsienaDV       9     4              0
## 46 Network           0 self$bipartite_rsienaDV       6     7              0
## 47 Network           0 self$bipartite_rsienaDV      11     3              0
## 48 Network           0 self$bipartite_rsienaDV       5     5              0
##    reciprocal_rate LogOptionSetProb LogChoiceProb diagonal stability tie_change
## 1       0.08333333        -2.484907     -2.160006    FALSE     FALSE       TRUE
## 2       0.08333333        -2.484907     -1.744893    FALSE     FALSE       TRUE
## 3       0.08333333        -2.484907     -1.882250    FALSE     FALSE       TRUE
## 4       0.08333333        -2.484907     -1.882250    FALSE     FALSE       TRUE
## 5       0.08333333        -2.484907     -1.842172    FALSE     FALSE       TRUE
## 6       0.08333333        -2.484907     -2.058715    FALSE      TRUE      FALSE
## 7       0.08333333        -2.484907     -1.717030    FALSE     FALSE       TRUE
## 8       0.08333333        -2.484907     -1.717030    FALSE     FALSE       TRUE
## 9       0.08333333        -2.484907     -1.755244    FALSE     FALSE       TRUE
## 10      0.08333333        -2.484907     -1.855504    FALSE     FALSE       TRUE
## 11      0.08333333        -2.484907     -2.450115    FALSE     FALSE       TRUE
## 12      0.08333333        -2.484907     -1.870035    FALSE     FALSE       TRUE
## 13      0.08333333        -2.484907     -1.972047    FALSE      TRUE      FALSE
## 14      0.08333333        -2.484907     -1.755244    FALSE     FALSE       TRUE
## 15      0.08333333        -2.484907     -2.146673    FALSE     FALSE       TRUE
## 16      0.08333333        -2.484907     -1.727381    FALSE     FALSE       TRUE
## 17      0.08333333        -2.484907     -1.886578    FALSE      TRUE      FALSE
## 18      0.08333333        -2.484907     -1.727381    FALSE     FALSE       TRUE
## 19      0.08333333        -2.484907     -2.146673    FALSE     FALSE       TRUE
## 20      0.08333333        -2.484907     -1.855504    FALSE     FALSE       TRUE
## 21      0.08333333        -2.484907     -1.972047    FALSE      TRUE      FALSE
## 22      0.08333333        -2.484907     -1.755244    FALSE     FALSE       TRUE
## 23      0.08333333        -2.484907     -1.910114    FALSE     FALSE       TRUE
## 24      0.08333333        -2.484907     -1.744893    FALSE     FALSE       TRUE
## 25      0.08333333        -2.484907     -1.886578    FALSE      TRUE      FALSE
## 26      0.08333333        -2.484907     -1.744893    FALSE     FALSE       TRUE
## 27      0.08333333        -2.484907     -2.174536    FALSE     FALSE       TRUE
## 28      0.08333333        -2.484907     -1.855504    FALSE     FALSE       TRUE
## 29      0.08333333        -2.484907     -1.972047    FALSE      TRUE      FALSE
## 30      0.08333333        -2.484907     -1.886578    FALSE      TRUE      FALSE
## 31      0.08333333        -2.484907     -1.740714    FALSE     FALSE       TRUE
## 32      0.08333333        -2.484907     -1.870035    FALSE     FALSE       TRUE
## 33      0.08333333        -2.484907     -1.972047    FALSE      TRUE      FALSE
## 34      0.08333333        -2.484907     -1.755244    FALSE     FALSE       TRUE
## 35      0.08333333        -2.484907     -1.910114    FALSE     FALSE       TRUE
## 36      0.08333333        -2.484907     -2.436783    FALSE     FALSE       TRUE
## 37      0.08333333        -2.484907     -1.910114    FALSE     FALSE       TRUE
## 38      0.08333333        -2.484907     -2.174536    FALSE     FALSE       TRUE
## 39      0.08333333        -2.484907     -1.882250    FALSE     FALSE       TRUE
## 40      0.08333333        -2.484907     -2.174536    FALSE     FALSE       TRUE
## 41      0.08333333        -2.484907     -2.058715    FALSE      TRUE      FALSE
## 42      0.08333333        -2.484907     -2.464646    FALSE     FALSE       TRUE
## 43      0.08333333        -2.484907     -2.146673    FALSE     FALSE       TRUE
## 44      0.08333333        -2.484907     -2.436783    FALSE     FALSE       TRUE
## 45      0.08333333        -2.484907     -1.717030    FALSE     FALSE       TRUE
## 46      0.08333333        -2.484907     -2.058715    FALSE      TRUE      FALSE
## 47      0.08333333        -2.484907     -1.855504    FALSE     FALSE       TRUE
## 48      0.08333333        -2.484907     -2.160006    FALSE     FALSE       TRUE
##    chain_step_id
## 1              1
## 2              2
## 3              3
## 4              4
## 5              5
## 6              5
## 7              7
## 8              8
## 9              9
## 10            10
## 11            11
## 12            12
## 13            12
## 14            14
## 15            15
## 16            16
## 17            16
## 18            18
## 19            19
## 20            20
## 21            20
## 22            22
## 23            23
## 24            24
## 25            24
## 26            26
## 27            27
## 28            28
## 29            28
## 30            28
## 31            31
## 32            32
## 33            32
## 34            34
## 35            35
## 36            36
## 37            37
## 38            38
## 39            39
## 40            40
## 41            40
## 42            42
## 43            43
## 44            44
## 45            45
## 46            45
## 47            47
## 48            48
## 
## =====================================================================
##                                                  Model 1             
## ---------------------------------------------------------------------
## Rate parameter period 1                           0.0650 (0.0605)    
## outdegree (density)                               0.0000 (0.0000)    
## indegree - popularity                             0.0000 (0.0000)    
## outdegree - activity                              0.0000 (0.0000)    
## self$component_1_coCovar alter                    1.0000 (0.0000) ***
## self$strat_1_coCovar ego                          0.1000 (0.0000) ***
## ind. pop.^(1/0.1) weighted self$strat_2_coCovar   0.1000 (0.0000) ***
## ---------------------------------------------------------------------
## Iterations                                       48                  
## =====================================================================
## *** p < 0.001; ** p < 0.01; * p < 0.05
## Run Rsiena search using variable parameters in theta_matrix
env1$bi_env_arr[,, c(1,dim(env1$bi_env_arr)[3]) ]
## , , 1
## 
##       [,1] [,2] [,3] [,4] [,5] [,6]
##  [1,]    0    0    0    0    0    0
##  [2,]    0    0    0    0    0    0
##  [3,]    0    0    0    0    0    0
##  [4,]    0    0    0    0    0    0
##  [5,]    0    0    0    0    1    0
##  [6,]    0    0    0    0    0    0
##  [7,]    0    0    0    0    0    0
##  [8,]    0    0    0    0    0    0
##  [9,]    0    0    0    0    0    0
## [10,]    0    0    0    0    0    0
## [11,]    0    0    0    0    1    0
## [12,]    0    0    0    0    0    0
## 
## , , 2
## 
##       [,1] [,2] [,3] [,4] [,5] [,6]
##  [1,]    1    0    0    1    0    0
##  [2,]    0    0    1    0    0    1
##  [3,]    1    0    0    0    1    0
##  [4,]    0    1    1    1    0    0
##  [5,]    0    0    0    0    0    0
##  [6,]    0    1    1    0    1    1
##  [7,]    1    1    0    0    0    1
##  [8,]    0    0    0    0    0    0
##  [9,]    1    0    0    1    0    1
## [10,]    1    0    1    1    1    0
## [11,]    0    1    1    0    1    0
## [12,]    1    1    0    0    1    0

1.5 Plot Actor Degrees (Component Scope and Common Affiliation Social Ties)

Time series is simulated decision steps.

##
env1$plot_actor_degrees(loess_span = 0.35)
## `geom_smooth()` using formula = 'y ~ x'
## `geom_smooth()` using formula = 'y ~ x'

1.6 Plot Component Degrees (Membership/Popularity and Common Actor Epistasis Ties)

##
env1$plot_component_degrees(loess_span = 0.35)
## `geom_smooth()` using formula = 'y ~ x'
## `geom_smooth()` using formula = 'y ~ x'

1.7 Plot Utility by Strategy over Time (Simulated Decision Steps)

## [1] 576   5
## `geom_smooth()` using formula = 'y ~ x'
## `geom_smooth()` using formula = 'y ~ x'

1.8 Plot Utility by Actor over Time (Simulated Decision Steps)

## [1] 576   5
## `geom_smooth()` using formula = 'y ~ x'
## `geom_smooth()` using formula = 'y ~ x'

1.9 Plot Contributions to Actor Utility colored by Strategy

Time series is simulated decision steps.

##
env1$plot_utility_components(use_contributions=TRUE, loess_span=0.35)
## `geom_smooth()` using formula = 'y ~ x'

1.10 Plot Actor Utility Timeseries and Distribution by Strategy

env1$plot_actor_utility_strategy_summary(loess_span=0.5)

1.11 Actor Utility Distribution by Landscape Distance

Number of changes to component ties as distance from counterfactual affiliation configurations

if (env1$N <= 16)
{
    
  ######################
  ##**TODO**
  
  self <- env1
  
  
  #
  config_param_vals <- c(
    unlist(sapply(self$config_structure_model$dv_bipartite$effects, function(x) x$parameter)),
    unlist(sapply(self$config_structure_model$dv_bipartite$coCovars, function(x) x$parameter)),
    unlist(sapply(self$config_structure_model$dv_bipartite$varCovars, function(x) x$parameter))
  )
  fixed_params <- c(
    unlist(sapply(self$config_structure_model$dv_bipartite$effects, function(x) x$fix)),
    unlist(sapply(self$config_structure_model$dv_bipartite$coCovars, function(x) x$fix)),
    unlist(sapply(self$config_structure_model$dv_bipartite$varCovars, function(x) x$fix))
  )
  #
  #UTILITY
  theta   <- self$rsiena_model$thetaUsed
  ## replace fixed theta param with given param values (instead of zero default when effect is fixed)
  # if (any(fixed_params)) theta[ fixed_params ] <- config_param_vals[ fixed_params ]
  
  
  
  ## PLOT ACTOR UTILITY LANDSCAPE
  utilist <- list()
  statsl <- list()
  pltlist <- list()
  
  # step_ids <- round(c(1,8,64,512, self$rsiena_model$n3/2, self$rsiena_model$n3))
  acpds <- seq(1, nrow(theta), by= self$M )
  step_ids <- c(acpds[1:min(3, length(acpds))])
  for (step_id in step_ids) {
    # step_id <- 1
    cat(sprintf('\nstep %s: actors ', step_id))
    
    bi_env_mat_step <- self$bi_env_arr[, , step_id]
    
    act_counterfacts <- list()
    
  
    for (i in 1:min(3, self$M)) {
      cat(sprintf(' %s ', i))
      
      
      ##**ACTOR i DECISION PERSPECTIVE** 0000000000000000000000000000000000
      
      ##**TODO**
      ## ALL actor-component counterfactual configuations for Actor i  (2^N rows by N cols)
      iland <- expand.grid(lapply(1:self$N, function(x) 0:1 ))
      iland_config_step_row_id <- which(apply(iland, 1, function(x) all(x == bi_env_mat_step[i,]) ))
      
      tmpmat <- bi_env_mat_step
      ## ifit dimensions [ M, 2^N ]
      ifit <- apply(iland, 1, function(x){
        tmpmat[i,] <- x  ## set counterfactual actor-component configuration
        config_fit <- if('matrix' %in% class(theta)) {
          self$get_struct_mod_stats_mat_from_bi_mat( tmpmat ) %*% theta[step_id,] ## variable thetas
        } else {
          self$get_struct_mod_stats_mat_from_bi_mat( tmpmat ) %*% theta   ## fixed thetas
        }
        return( config_fit ) ## compute utility vector
      }) 
      
      # apply
      
      ids.max <- which(ifit == max(ifit), arr.ind = TRUE) 
      fits.max <- ifit[ ids.max ]
      nmax <- length(fits.max)
      
      # ifit
      
      # ids.max[1,]
      
      act_counterfacts[[i]] <- list(
        ifit = ifit, #Given all other ties, Actor i's configurations applied to utility func for all actors
        ids.max = ids.max,
        fits.max = fits.max,
        nmax = nmax
      )
      
      ## distances of each counterfactual configuration 
      dist_counterfac <- iland - bi_env_mat_step[i, ]
      z <- apply( dist_counterfac, 1, function(x) {
        c(dist=sum(x!=0),
          `drop`=sum(x==-1), 
          `nochange`=sum(x==0),
          `add`=sum(x==1), 
          counterfac=paste(x, collapse = '|'), 
          start=paste(bi_env_mat_step[i, ], collapse = '|') 
          )
      })
      # z <- z[,order(z['d',], decreasing = F)]
      ##**TODO**
      ##**INFORMATION LOSS**
      ##  This uses only ego's own counterfactual fits (landscape)
      ##  but the corresponding other actor's affected fits (landscapes are not currently used )
      z <- rbind(z, utility_ego=ifit[ i , ] )
      z <- rbind(z, utility_alter_mean= colMeans(ifit[ -i , ], na.rm = T) )
      z <- rbind(z, utility_alter_sd  = apply(ifit[ -i , ], 2, function(x) sd(x, na.rm = T)) )
      
      wdf <- as.data.frame( t(z) )
      
      actfit_long <- wdf %>% 
        mutate(
          dist = as.numeric(dist),
          drop = as.numeric(drop),
          nochange = as.numeric(nochange),
          add = as.numeric(add) ,
          utility_ego=as.numeric(utility_ego),
          utility_alter_mean = as.numeric(utility_alter_mean),
          utility_alter_sd = as.numeric(utility_alter_sd)
        ) %>%
        pivot_longer(cols = c( drop:add, utility_ego:utility_alter_sd )) 
       
      
      plt <- actfit_long %>% ggplot(aes(x=value))+ 
        # geom_density() + 
        geom_histogram() +
        facet_grid(dist ~ name, scales='free_x') + theme_bw() + 
        ggtitle(sprintf('Utility Transition Paths: Actor %s, Step %s', i, step_id))
      
      pltlist[[ length(pltlist)+1 ]] <- plt
      
      # z <- apply(iland, 1, function(x) {
      #   x <- plyr::count( x - bi_env_mat_step[i, ]) 
      #   cnts <- merge(x$freq, data.frame(x=c())
      #   names(cnts) <- x$x
      #   return(cnts)
      # }, simplify = T)
      # 
      # stringdist::stringdist( , , method='hamming')
      
      step_actor_key <- sprintf('%s|%s',step_id,i)
      
      statsl[[step_actor_key]] <- list(
        actfit_long = actfit_long,
        act_counterfacts = act_counterfacts,
        plt = plt,
        actor_id=i, chain_step_id = step_id,
        strategy=as.factor(self$strat_1_coCovar[i])
      )
      
      actor_strats <- env1$get_actor_strategies()
      
      utilist[[step_actor_key]] <- actfit_long %>% 
        filter(name %in% c('utility_ego','utility_alter_mean')) %>%
        mutate(actor_id=i, chain_step_id = step_id, strategy=as.factor(actor_strats[i]))
      
    } ##/end actor loop
    
    # # Arrange plots across multiple pages
    # multi_page_plot <- marrangeGrob(pltlist, nrow = 1, ncol = 1)
    # 
    # # Save to a PDF
    # ggsave("multi_page_plots.pdf", multi_page_plot, width = 8, height = 8)
    
  } ##/end step loop
  
  #
  utildf <- data.table::rbindlist(utilist, use.names = T, idcol = 'step_actor_key')
  
  
  ##--------------------------------------------------------------
  ## 4.  Actor Utility Transition Paths
  ##--------------------------------------------------------------
  # Arrange plots across multiple pages
  plt_act_paths <- marrangeGrob(pltlist, nrow = 1, ncol = 1)
  plt_act_paths
  # # Save to a PDF
  # actor_path_plotfile <- sprintf('%s_actor_pd_dist2peaks.pdf', self$UUID)
  # plot_dir <- getwd()
  # ggsave(filename = file.path(plot_dir, actor_path_plotfile), plt_act_paths, 
  #        width = 8, height = 8)

  
} 
## 
## step 1: actors  1  2  3 
## step 13: actors  1  2  3 
## step 25: actors  1  2  3
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

2. Environmental WITH Endogenous Noise

Structural Effects in range [-1, 0.4]

2.1 Environmental Configuration

Reuse existing environ_config

2.2 Structrual Model

SAOM Objective Function serves as the stochastic actor’s utility function for strategic search.

strategies <- list(
  egoX   =   c(-1, 0, 1),
  inPopX =   c( 1, 0, -1)
)


## 2.b. Component Payoffs vector
set.seed(12345)
component_payoffs <-  runif(environ_params$N, min = 0, max = 1)
## 2. Strategies sets the objective function as a linear combination of network stats across DVs
#
actor_strats_list <- lapply(strategies, function(strat) rep(strat,  environ_params$M/length(strat)) )

#
structure_model <- list(
  dv_bipartite = list(
    name = 'self$bipartite_rsienaDV',
    effects = list( ##**STRUCTURAL EFFECTS -- dyadic/network endogeneity sources**
      list(effect='density', parameter= -1, fix=T, dv_name=DV_NAME), ##interaction1 = NULL
      list(effect='inPop',   parameter= .1, fix=T, dv_name=DV_NAME), #interaction1 = NUL
      list(effect='outAct',  parameter= .1, fix=T, dv_name=DV_NAME)
    ),
    ## COVARIATE EFFECTS
    coCovars = list( 
      ##** COMPONENTS : MONADIC CONSTANT COVARIATE EFFECTS **##
      list(effect='altX',   parameter= 1,  fix=T,dv_name=DV_NAME, 
           interaction1='self$component_1_coCovar', x = component_payoffs 
      ),
      ##** STRATEGIES : MONADIC CONSTANT COVARIATE EFFECTS **##
      list(effect='egoX',   parameter= .1,  fix=T,dv_name=DV_NAME, 
           interaction1='self$strat_1_coCovar',     x = actor_strats_list[[1]] 
      ), #interaction1 = NULL
      list(effect='inPopX', parameter= .1,  fix=T,dv_name=DV_NAME, 
           interaction1='self$strat_2_coCovar',  x = actor_strats_list[[2]] 
      )
    ),
    varCovars = list() ##**MONADIC TIME-VARYING COVARIATE EFFECTS -- DYNAMIC STRATEGY PROGRAMS**
  )
)

2.3 Run RSiena Search Process

## Run Rsiena search using variable parameters in theta_matrix
env1$search_rsiena(
  structure_model,
  iterations = env1$M * 4,
  digits = 4,
  run_seed = 12345
)
## 
## 
##  theta_matrix : 
## 
##       density inPop outAct altX egoX inPopX
##  [1,]      -1   0.1    0.1    1  0.1    0.1
##  [2,]      -1   0.1    0.1    1  0.1    0.1
##  [3,]      -1   0.1    0.1    1  0.1    0.1
##  [4,]      -1   0.1    0.1    1  0.1    0.1
##  [5,]      -1   0.1    0.1    1  0.1    0.1
##  [6,]      -1   0.1    0.1    1  0.1    0.1
##  [7,]      -1   0.1    0.1    1  0.1    0.1
##  [8,]      -1   0.1    0.1    1  0.1    0.1
##  [9,]      -1   0.1    0.1    1  0.1    0.1
## [10,]      -1   0.1    0.1    1  0.1    0.1
## [11,]      -1   0.1    0.1    1  0.1    0.1
## [12,]      -1   0.1    0.1    1  0.1    0.1
## [13,]      -1   0.1    0.1    1  0.1    0.1
## [14,]      -1   0.1    0.1    1  0.1    0.1
## [15,]      -1   0.1    0.1    1  0.1    0.1
## [16,]      -1   0.1    0.1    1  0.1    0.1
## [17,]      -1   0.1    0.1    1  0.1    0.1
## [18,]      -1   0.1    0.1    1  0.1    0.1
## [19,]      -1   0.1    0.1    1  0.1    0.1
## [20,]      -1   0.1    0.1    1  0.1    0.1
## [21,]      -1   0.1    0.1    1  0.1    0.1
## [22,]      -1   0.1    0.1    1  0.1    0.1
## [23,]      -1   0.1    0.1    1  0.1    0.1
## [24,]      -1   0.1    0.1    1  0.1    0.1
## [25,]      -1   0.1    0.1    1  0.1    0.1
## [26,]      -1   0.1    0.1    1  0.1    0.1
## [27,]      -1   0.1    0.1    1  0.1    0.1
## [28,]      -1   0.1    0.1    1  0.1    0.1
## [29,]      -1   0.1    0.1    1  0.1    0.1
## [30,]      -1   0.1    0.1    1  0.1    0.1
## [31,]      -1   0.1    0.1    1  0.1    0.1
## [32,]      -1   0.1    0.1    1  0.1    0.1
## [33,]      -1   0.1    0.1    1  0.1    0.1
## [34,]      -1   0.1    0.1    1  0.1    0.1
## [35,]      -1   0.1    0.1    1  0.1    0.1
## [36,]      -1   0.1    0.1    1  0.1    0.1
## [37,]      -1   0.1    0.1    1  0.1    0.1
## [38,]      -1   0.1    0.1    1  0.1    0.1
## [39,]      -1   0.1    0.1    1  0.1    0.1
## [40,]      -1   0.1    0.1    1  0.1    0.1
## [41,]      -1   0.1    0.1    1  0.1    0.1
## [42,]      -1   0.1    0.1    1  0.1    0.1
## [43,]      -1   0.1    0.1    1  0.1    0.1
## [44,]      -1   0.1    0.1    1  0.1    0.1
## [45,]      -1   0.1    0.1    1  0.1    0.1
## [46,]      -1   0.1    0.1    1  0.1    0.1
## [47,]      -1   0.1    0.1    1  0.1    0.1
## [48,]      -1   0.1    0.1    1  0.1    0.1
## 
## 
## self$rsiena_data : 
## 
## Dependent variables:  self$bipartite_rsienaDV 
## Number of observations: 2 
## 
## Nodesets                 ACTORS      COMPONENTS 
## Number of nodes              12               6 
## 
## Dependent variable self$bipartite_rsienaDV
## Type               bipartite              
## Observations       2                      
## First nodeset      ACTORS                 
## Second nodeset     COMPONENTS             
## Densities          0.014 0.014            
## 
## Constant covariates:  self$component_1_coCovar, self$strat_1_coCovar, self$strat_2_coCovar 
## 
##  structural effects i=1, j=1
## $effect
## [1] "density"
## 
## $parameter
## [1] -1
## 
## $fix
## [1] TRUE
## 
## $dv_name
## [1] "self$bipartite_rsienaDV"
## 
##   effectName          include fix  test  initialValue parm
## 1 outdegree (density) TRUE    TRUE FALSE          0   0   
##   effectName          include fix  test  initialValue parm
## 1 outdegree (density) TRUE    TRUE FALSE          0   -1  
## 
##  structural effects i=1, j=2
## $effect
## [1] "inPop"
## 
## $parameter
## [1] 0.1
## 
## $fix
## [1] TRUE
## 
## $dv_name
## [1] "self$bipartite_rsienaDV"
## 
##   effectName            include fix  test  initialValue parm
## 1 indegree - popularity TRUE    TRUE FALSE          0   0   
##   effectName            include fix  test  initialValue parm
## 1 indegree - popularity TRUE    TRUE FALSE          0   0.1 
## 
##  structural effects i=1, j=3
## $effect
## [1] "outAct"
## 
## $parameter
## [1] 0.1
## 
## $fix
## [1] TRUE
## 
## $dv_name
## [1] "self$bipartite_rsienaDV"
## 
##   effectName           include fix  test  initialValue parm
## 1 outdegree - activity TRUE    TRUE FALSE          0   0   
##   effectName           include fix  test  initialValue parm
## 1 outdegree - activity TRUE    TRUE FALSE          0   0.1 
## 
##  coCovars i=1, j=1
## $effect
## [1] "altX"
## 
## $parameter
## [1] 1
## 
## $fix
## [1] TRUE
## 
## $dv_name
## [1] "self$bipartite_rsienaDV"
## 
## $interaction1
## [1] "self$component_1_coCovar"
## 
## $x
## [1] 0.7209039 0.8757732 0.7609823 0.8861246 0.4564810 0.1663718
## 
##   effectName                     include fix  test  initialValue parm
## 1 self$component_1_coCovar alter TRUE    TRUE FALSE          0   0   
##   effectName                     include fix  test  initialValue parm
## 1 self$component_1_coCovar alter TRUE    TRUE FALSE          0   1   
## 
##  coCovars i=1, j=2
## $effect
## [1] "egoX"
## 
## $parameter
## [1] 0.1
## 
## $fix
## [1] TRUE
## 
## $dv_name
## [1] "self$bipartite_rsienaDV"
## 
## $interaction1
## [1] "self$strat_1_coCovar"
## 
## $x
##  [1] -1  0  1 -1  0  1 -1  0  1 -1  0  1
## 
##   effectName               include fix  test  initialValue parm
## 1 self$strat_1_coCovar ego TRUE    TRUE FALSE          0   0   
##   effectName               include fix  test  initialValue parm
## 1 self$strat_1_coCovar ego TRUE    TRUE FALSE          0   0.1 
## 
##  coCovars i=1, j=3
## $effect
## [1] "inPopX"
## 
## $parameter
## [1] 0.1
## 
## $fix
## [1] TRUE
## 
## $dv_name
## [1] "self$bipartite_rsienaDV"
## 
## $interaction1
## [1] "self$strat_2_coCovar"
## 
## $x
##  [1]  1  0 -1  1  0 -1  1  0 -1  1  0 -1
## 
##   effectName                                    include fix  test  initialValue
## 1 ind. pop.^(1/#) weighted self$strat_2_coCovar TRUE    TRUE FALSE          0  
##   parm
## 1 1   
##   effectName                                    include fix  test  initialValue
## 1 ind. pop.^(1/#) weighted self$strat_2_coCovar TRUE    TRUE FALSE          0  
##   parm
## 1 0.1 
## If you use this algorithm object, siena07 will create/use an output file C:/Users/sdr8y/OneDrive - University of Missouri/Research/Search_networks/SaoMNK/R/_test_tutorial_nb__173813296720.txt .
## 
## Start phase 0 
## theta: 0 0 0 0 0 0 
## 
## Start phase 3 
## Parameter values used for simulations
## 
##                                                           Mean      Standard      
##                                                             value   Deviation     
## 
## Rate parameters: 
##   0       Rate parameter                                    NA    ( NA        )   
## 
## Other parameters: 
##   1. eval outdegree (density)                             -1.0    (  0        )   
##   2. eval indegree - popularity                            0.1    (  0        )   
##   3. eval outdegree - activity                             0.1    (  0        )   
##   4. eval self$component_1_coCovar alter                   1.0    (  0        )   
##   5. eval self$strat_1_coCovar ego                         0.1    (  0        )   
##   6. eval ind. pop.^(1/0.1) weighted self$strat_2_coCovar  0.1    (  0        )   
## 
## Simulated means and standard deviations
##   1. Number of ties                                             1.604    0.536 
##   2. Sum of squared indegrees                                   1.729    0.792 
##   3. Sum of squared outdegrees                                  1.688    0.719 
##   4. Sum of indegrees x self$component_1_coCovar               -0.154    0.195 
##   5. Sum of outdegrees x self$strat_1_coCovar                  -0.062    0.727 
##   6. indegree pop.^(1/0.1) weighted self$strat_2_coCovar        0.042    0.849 
## 
## 
## Simulated statistics are in x$sf
## and simulated dependent variables in x$sims, where x is the created object.
## 
## Total of 48 iteration steps.
## 
## Covariance matrix of estimates (correlations below diagonal)
## 
##            0            0            0            0            0            0
##          NaN            0            0            0            0            0
##          NaN          NaN            0            0            0            0
##          NaN          NaN          NaN            0            0            0
##          NaN          NaN          NaN          NaN            0            0
##          NaN          NaN          NaN          NaN          NaN            0
## 
## Derivative matrix of expected statistics X by parameters:
## 
##        0.270        0.313        0.357        0.014       -0.032        0.026
##        0.293        0.452        0.384        0.002       -0.008       -0.017
##        0.318        0.357        0.496        0.021       -0.030        0.025
##        0.007       -0.020        0.018        0.036       -0.021        0.025
##        0.001        0.016        0.005       -0.004        0.135       -0.152
##        0.000        0.000        0.000        0.000        0.000        0.000
## 
## Covariance matrix of X (correlations below diagonal):
## 
##        0.287        0.337        0.320        0.006       -0.025        0.017
##        0.795        0.627        0.360       -0.023        0.025       -0.074
##        0.832        0.633        0.517        0.023       -0.020        0.013
##        0.054       -0.147        0.167        0.038       -0.020        0.025
##       -0.065        0.044       -0.038       -0.143        0.528       -0.593
##        0.037       -0.109        0.022        0.151       -0.961        0.722
## 
## 
## 
## Simulated Decision Chain Summary:
## 
##    dv_type           dv_type_bin  dv_varname           id_from      
##  Length:48          Min.   :0    Length:48          Min.   : 1.000  
##  Class :character   1st Qu.:0    Class :character   1st Qu.: 4.000  
##  Mode  :character   Median :0    Mode  :character   Median : 7.000  
##                     Mean   :0                       Mean   : 6.708  
##                     3rd Qu.:0                       3rd Qu.: 9.250  
##                     Max.   :0                       Max.   :12.000  
##                                                                     
##      id_to       beh_difference reciprocal_rate   LogOptionSetProb
##  Min.   :1.000   Min.   :0      Min.   :0.08333   Min.   :-2.485  
##  1st Qu.:2.750   1st Qu.:0      1st Qu.:0.08333   1st Qu.:-2.485  
##  Median :5.000   Median :0      Median :0.08333   Median :-2.485  
##  Mean   :4.583   Mean   :0      Mean   :0.08333   Mean   :-2.485  
##  3rd Qu.:7.000   3rd Qu.:0      3rd Qu.:0.08333   3rd Qu.:-2.485  
##  Max.   :7.000   Max.   :0      Max.   :0.08333   Max.   :-2.485  
##                                                                   
##  LogChoiceProb      diagonal         stability       tie_change     
##  Min.   :-2.645   Length:48          Mode :logical   Mode :logical  
##  1st Qu.:-2.050   Class :character   FALSE:31        FALSE:17       
##  Median :-1.930   Mode  :character   TRUE :17        TRUE :31       
##  Mean   :-1.813                                                     
##  3rd Qu.:-1.396                                                     
##  Max.   :-0.903                                                     
##                                                                     
##  chain_step_id  
##  Min.   : 2.00  
##  1st Qu.:13.00  
##  Median :24.00  
##  Mean   :24.51  
##  3rd Qu.:36.00  
##  Max.   :48.00  
##  NA's   :1      
## [1] 48 13
##    dv_type dv_type_bin              dv_varname id_from id_to beh_difference
## 1  Network           0 self$bipartite_rsienaDV      11     7              0
## 2  Network           0 self$bipartite_rsienaDV       4     4              0
## 3  Network           0 self$bipartite_rsienaDV      12     1              0
## 4  Network           0 self$bipartite_rsienaDV       9     1              0
## 5  Network           0 self$bipartite_rsienaDV       6     3              0
## 6  Network           0 self$bipartite_rsienaDV       3     7              0
## 7  Network           0 self$bipartite_rsienaDV       9     5              0
## 8  Network           0 self$bipartite_rsienaDV       9     4              0
## 9  Network           0 self$bipartite_rsienaDV       1     2              0
## 10 Network           0 self$bipartite_rsienaDV       5     4              0
## 11 Network           0 self$bipartite_rsienaDV       2     7              0
## 12 Network           0 self$bipartite_rsienaDV       4     3              0
## 13 Network           0 self$bipartite_rsienaDV       8     7              0
## 14 Network           0 self$bipartite_rsienaDV       4     2              0
## 15 Network           0 self$bipartite_rsienaDV       6     6              0
## 16 Network           0 self$bipartite_rsienaDV       6     2              0
## 17 Network           0 self$bipartite_rsienaDV       4     7              0
## 18 Network           0 self$bipartite_rsienaDV      12     2              0
## 19 Network           0 self$bipartite_rsienaDV      12     6              0
## 20 Network           0 self$bipartite_rsienaDV       5     4              0
## 21 Network           0 self$bipartite_rsienaDV       8     7              0
## 22 Network           0 self$bipartite_rsienaDV       7     2              0
## 23 Network           0 self$bipartite_rsienaDV       1     2              0
## 24 Network           0 self$bipartite_rsienaDV      10     4              0
## 25 Network           0 self$bipartite_rsienaDV       1     7              0
## 26 Network           0 self$bipartite_rsienaDV       1     5              0
## 27 Network           0 self$bipartite_rsienaDV       7     7              0
## 28 Network           0 self$bipartite_rsienaDV       2     3              0
## 29 Network           0 self$bipartite_rsienaDV       8     7              0
## 30 Network           0 self$bipartite_rsienaDV       7     7              0
## 31 Network           0 self$bipartite_rsienaDV      11     2              0
## 32 Network           0 self$bipartite_rsienaDV      10     3              0
## 33 Network           0 self$bipartite_rsienaDV      11     7              0
## 34 Network           0 self$bipartite_rsienaDV       1     3              0
## 35 Network           0 self$bipartite_rsienaDV       7     1              0
## 36 Network           0 self$bipartite_rsienaDV       9     7              0
## 37 Network           0 self$bipartite_rsienaDV      10     1              0
## 38 Network           0 self$bipartite_rsienaDV      10     6              0
## 39 Network           0 self$bipartite_rsienaDV       3     1              0
## 40 Network           0 self$bipartite_rsienaDV       7     6              0
## 41 Network           0 self$bipartite_rsienaDV      12     7              0
## 42 Network           0 self$bipartite_rsienaDV       7     7              0
## 43 Network           0 self$bipartite_rsienaDV       3     7              0
## 44 Network           0 self$bipartite_rsienaDV       6     7              0
## 45 Network           0 self$bipartite_rsienaDV       9     5              0
## 46 Network           0 self$bipartite_rsienaDV       6     7              0
## 47 Network           0 self$bipartite_rsienaDV      11     3              0
## 48 Network           0 self$bipartite_rsienaDV       5     5              0
##    reciprocal_rate LogOptionSetProb LogChoiceProb diagonal stability tie_change
## 1       0.08333333        -2.484907    -1.3398382    FALSE      TRUE      FALSE
## 2       0.08333333        -2.484907    -1.9253236    FALSE     FALSE       TRUE
## 3       0.08333333        -2.484907    -2.0381360    FALSE     FALSE       TRUE
## 4       0.08333333        -2.484907    -2.0381360    FALSE     FALSE       TRUE
## 5       0.08333333        -2.484907    -1.9980576    FALSE     FALSE       TRUE
## 6       0.08333333        -2.484907    -1.4146004    FALSE      TRUE      FALSE
## 7       0.08333333        -2.484907    -2.2025589    FALSE     FALSE       TRUE
## 8       0.08333333        -2.484907    -1.8729153    FALSE     FALSE       TRUE
## 9       0.08333333        -2.484907    -1.9356750    FALSE     FALSE       TRUE
## 10      0.08333333        -2.484907    -2.2492524    FALSE     FALSE       TRUE
## 11      0.08333333        -2.484907    -1.3398382    FALSE      TRUE      FALSE
## 12      0.08333333        -2.484907    -2.0504658    FALSE     FALSE       TRUE
## 13      0.08333333        -2.484907    -1.3398382    FALSE      TRUE      FALSE
## 14      0.08333333        -2.484907    -1.9356750    FALSE     FALSE       TRUE
## 15      0.08333333        -2.484907    -2.5926681    FALSE     FALSE       TRUE
## 16      0.08333333        -2.484907    -1.8832667    FALSE     FALSE       TRUE
## 17      0.08333333        -2.484907    -1.2670087    FALSE      TRUE      FALSE
## 18      0.08333333        -2.484907    -1.8832667    FALSE     FALSE       TRUE
## 19      0.08333333        -2.484907    -2.5926681    FALSE     FALSE       TRUE
## 20      0.08333333        -2.484907    -2.2492524    FALSE     FALSE       TRUE
## 21      0.08333333        -2.484907    -1.3398382    FALSE      TRUE      FALSE
## 22      0.08333333        -2.484907    -1.9356750    FALSE     FALSE       TRUE
## 23      0.08333333        -2.484907    -1.9356750    FALSE     FALSE       TRUE
## 24      0.08333333        -2.484907    -1.9253236    FALSE     FALSE       TRUE
## 25      0.08333333        -2.484907    -1.2670087    FALSE      TRUE      FALSE
## 26      0.08333333        -2.484907    -2.2549672    FALSE     FALSE       TRUE
## 27      0.08333333        -2.484907    -1.2670087    FALSE      TRUE      FALSE
## 28      0.08333333        -2.484907    -2.0232953    FALSE     FALSE       TRUE
## 29      0.08333333        -2.484907    -1.3398382    FALSE      TRUE      FALSE
## 30      0.08333333        -2.484907    -1.2670087    FALSE      TRUE      FALSE
## 31      0.08333333        -2.484907    -1.9085045    FALSE     FALSE       TRUE
## 32      0.08333333        -2.484907    -2.0504658    FALSE     FALSE       TRUE
## 33      0.08333333        -2.484907    -1.3398382    FALSE      TRUE      FALSE
## 34      0.08333333        -2.484907    -2.0504658    FALSE     FALSE       TRUE
## 35      0.08333333        -2.484907    -2.0905443    FALSE     FALSE       TRUE
## 36      0.08333333        -2.484907    -1.4146004    FALSE      TRUE      FALSE
## 37      0.08333333        -2.484907    -2.0905443    FALSE     FALSE       TRUE
## 38      0.08333333        -2.484907    -2.6450764    FALSE     FALSE       TRUE
## 39      0.08333333        -2.484907    -2.0381360    FALSE     FALSE       TRUE
## 40      0.08333333        -2.484907    -2.6450764    FALSE     FALSE       TRUE
## 41      0.08333333        -2.484907    -1.4146004    FALSE      TRUE      FALSE
## 42      0.08333333        -2.484907    -1.2670087    FALSE      TRUE      FALSE
## 43      0.08333333        -2.484907    -1.4146004    FALSE      TRUE      FALSE
## 44      0.08333333        -2.484907    -1.4146004    FALSE      TRUE      FALSE
## 45      0.08333333        -2.484907    -2.2025589    FALSE     FALSE       TRUE
## 46      0.08333333        -2.484907    -1.4146004    FALSE      TRUE      FALSE
## 47      0.08333333        -2.484907    -2.0232953    FALSE     FALSE       TRUE
## 48      0.08333333        -2.484907    -0.9029791    FALSE     FALSE       TRUE
##    chain_step_id
## 1             NA
## 2              2
## 3              3
## 4              4
## 5              5
## 6              5
## 7              7
## 8              8
## 9              9
## 10            10
## 11            10
## 12            12
## 13            12
## 14            14
## 15            15
## 16            16
## 17            16
## 18            18
## 19            19
## 20            20
## 21            20
## 22            22
## 23            23
## 24            24
## 25            24
## 26            26
## 27            26
## 28            28
## 29            28
## 30            28
## 31            31
## 32            32
## 33            32
## 34            34
## 35            35
## 36            35
## 37            37
## 38            38
## 39            39
## 40            40
## 41            40
## 42            40
## 43            40
## 44            40
## 45            45
## 46            45
## 47            47
## 48            48
## 
## =====================================================================
##                                                  Model 1             
## ---------------------------------------------------------------------
## Rate parameter period 1                           0.0650 (0.0605)    
## outdegree (density)                              -1.0000 (0.0000) ***
## indegree - popularity                             0.1000 (0.0000) ***
## outdegree - activity                              0.1000 (0.0000) ***
## self$component_1_coCovar alter                    1.0000 (0.0000) ***
## self$strat_1_coCovar ego                          0.1000 (0.0000) ***
## ind. pop.^(1/0.1) weighted self$strat_2_coCovar   0.1000 (0.0000) ***
## ---------------------------------------------------------------------
## Iterations                                       48                  
## =====================================================================
## *** p < 0.001; ** p < 0.01; * p < 0.05

2.5 Plot Actor Degrees (Component Scope and Common Affiliation Social Ties)

Time series is simulated decision steps.

##
env1$plot_actor_degrees(loess_span = 0.25)
## `geom_smooth()` using formula = 'y ~ x'
## `geom_smooth()` using formula = 'y ~ x'

2.6 Plot Component Degrees (Membership/Popularity and Common Actor Epistasis Ties)

##
env1$plot_component_degrees(loess_span = 0.35)
## `geom_smooth()` using formula = 'y ~ x'
## `geom_smooth()` using formula = 'y ~ x'

2.7 Plot Utility by Strategy over Time (Simulated Decision Steps)

## [1] 576   5
## `geom_smooth()` using formula = 'y ~ x'
## `geom_smooth()` using formula = 'y ~ x'

2.8 Plot Utility by Actor over Time (Simulated Decision Steps)

## [1] 576   5
## `geom_smooth()` using formula = 'y ~ x'
## `geom_smooth()` using formula = 'y ~ x'

2.9 Plot Contributions to Actor Utility colored by Strategy

Time series is simulated decision steps.

##
env1$plot_utility_components(use_contributions=T, loess_span=0.35)
## `geom_smooth()` using formula = 'y ~ x'

2.10 Plot Actor Utility Timeseries and Distribution by Strategy

env1$plot_actor_utility_strategy_summary()

2.11 Actor Utility Distribution by Landscape Distance

Number of changes to component ties as distance from counterfactual affiliation configurations

if (env1$N <= 16)
{
  
  ######################
  ##**TODO**
  
  self <- env1
  
  
  #
  config_param_vals <- c(
    unlist(sapply(self$config_structure_model$dv_bipartite$effects, function(x) x$parameter)),
    unlist(sapply(self$config_structure_model$dv_bipartite$coCovars, function(x) x$parameter)),
    unlist(sapply(self$config_structure_model$dv_bipartite$varCovars, function(x) x$parameter))
  )
  fixed_params <- c(
    unlist(sapply(self$config_structure_model$dv_bipartite$effects, function(x) x$fix)),
    unlist(sapply(self$config_structure_model$dv_bipartite$coCovars, function(x) x$fix)),
    unlist(sapply(self$config_structure_model$dv_bipartite$varCovars, function(x) x$fix))
  )
  #
  #UTILITY
  theta   <- self$rsiena_model$thetaUsed
  ## replace fixed theta param with given param values (instead of zero default when effect is fixed)
  # if (any(fixed_params)) theta[ fixed_params ] <- config_param_vals[ fixed_params ]
  
  
  
  ## PLOT ACTOR UTILITY LANDSCAPE
  utilist <- list()
  statsl <- list()
  pltlist <- list()
  
  # step_ids <- round(c(1,8,64,512, self$rsiena_model$n3/2, self$rsiena_model$n3))

  
    # step_ids <- round(c(1,8,64,512, self$rsiena_model$n3/2, self$rsiena_model$n3))
  acpds <- seq(1, nrow(theta), by= self$M )
  step_ids <- c(acpds[1:min(3, length(acpds))])
  for (step_id in step_ids) {
    # step_id <- 1
    cat(sprintf('\nstep %s: actors ', step_id))
    
    bi_env_mat_step <- self$bi_env_arr[, , step_id]
    
    act_counterfacts <- list()
    
  
    for (i in 1:min(3, self$M)) {
      cat(sprintf(' %s ', i))
      
      
      ##**ACTOR i DECISION PERSPECTIVE** 0000000000000000000000000000000000
      
      ##**TODO**
      ## ALL actor-component counterfactual configuations for Actor i  (2^N rows by N cols)
      iland <- expand.grid(lapply(1:self$N, function(x) 0:1 ))
      iland_config_step_row_id <- which(apply(iland, 1, function(x) all(x == bi_env_mat_step[i,]) ))
      
      tmpmat <- bi_env_mat_step
      ## ifit dimensions [ M, 2^N ]
      ifit <- apply(iland, 1, function(x){
        tmpmat[i,] <- x  ## set counterfactual actor-component configuration
        config_fit <- if('matrix' %in% class(theta)) {
          self$get_struct_mod_stats_mat_from_bi_mat( tmpmat ) %*% theta[step_id,]
        } else {
          self$get_struct_mod_stats_mat_from_bi_mat( tmpmat ) %*% theta
        }
        return( config_fit ) ## compute utility vector
      }) 
      
      # apply
      
      ids.max <- which(ifit == max(ifit), arr.ind = TRUE) 
      fits.max <- ifit[ ids.max ]
      nmax <- length(fits.max)
      
      # ifit
      
      # ids.max[1,]
      
      act_counterfacts[[i]] <- list(
        ifit = ifit, #Given all other ties, Actor i's configurations applied to utility func for all actors
        ids.max = ids.max,
        fits.max = fits.max,
        nmax = nmax
      )
      
      ## distances of each counterfactual configuration 
      dist_counterfac <- iland - bi_env_mat_step[i, ]
      z <- apply( dist_counterfac, 1, function(x) {
        c(dist=sum(x!=0),`drop`=sum(x==-1), `nochange`=sum(x==0), `add`=sum(x==1), 
          counterfac=paste(x, collapse = '|'), start=paste(bi_env_mat_step[i, ], collapse = '|') )
      })
      # z <- z[,order(z['d',], decreasing = F)]
      ##**TODO**
      ##**INFORMATION LOSS**
      ##  This uses only ego's own counterfactual fits (landscape)
      ##  but the corresponding other actor's affected fits (landscapes are not currently used )
      z <- rbind(z, utility_ego=ifit[ i , ] )
      z <- rbind(z, utility_alter_mean= colMeans(ifit[ -i , ], na.rm = T) )
      z <- rbind(z, utility_alter_sd  = apply(ifit[ -i , ], 2, function(x) sd(x, na.rm = T)) )
      
      wdf <- as.data.frame( t(z) )
      
      actfit_long <- wdf %>% 
        mutate(
          dist = as.numeric(dist),
          drop = as.numeric(drop),
          nochange = as.numeric(nochange),
          add = as.numeric(add) ,
          utility_ego=as.numeric(utility_ego),
          utility_alter_mean = as.numeric(utility_alter_mean),
          utility_alter_sd = as.numeric(utility_alter_sd)
        ) %>%
        pivot_longer(cols = c( drop:add, utility_ego:utility_alter_sd )) 
       
      
      plt <- actfit_long %>% ggplot(aes(x=value))+ 
        # geom_density() + 
        geom_histogram() +
        facet_grid(dist ~ name, scales='free_x') + theme_bw() + 
        ggtitle(sprintf('Utility Transition Paths: Actor %s, Step %s', i, step_id))
      
      pltlist[[ length(pltlist)+1 ]] <- plt
      
      # z <- apply(iland, 1, function(x) {
      #   x <- plyr::count( x - bi_env_mat_step[i, ]) 
      #   cnts <- merge(x$freq, data.frame(x=c())
      #   names(cnts) <- x$x
      #   return(cnts)
      # }, simplify = T)
      # 
      # stringdist::stringdist( , , method='hamming')
      
      step_actor_key <- sprintf('%s|%s',step_id,i)
      
      statsl[[step_actor_key]] <- list(
        actfit_long = actfit_long,
        act_counterfacts = act_counterfacts,
        plt = plt,
        actor_id=i, chain_step_id = step_id,
        strategy=as.factor(self$strat_1_coCovar[i])
      )
      
      utilist[[step_actor_key]] <- actfit_long %>% 
        filter(name %in% c('utility_ego','utility_alter_mean')) %>%
        mutate(actor_id=i, chain_step_id = step_id, strategy=as.factor(self$strat_1_coCovar[i]))
      
    } ##/end actor loop
    
    # # Arrange plots across multiple pages
    # multi_page_plot <- marrangeGrob(pltlist, nrow = 1, ncol = 1)
    # 
    # # Save to a PDF
    # ggsave("multi_page_plots.pdf", multi_page_plot, width = 8, height = 8)
    
  } ##/end step loop
  
  #
  utildf <- data.table::rbindlist(utilist, use.names = T, idcol = 'step_actor_key')
  
  
  ##--------------------------------------------------------------
  ## 4.  Actor Utility Transition Paths
  ##--------------------------------------------------------------
  # Arrange plots across multiple pages
  plt_act_paths <- marrangeGrob(pltlist, nrow = 1, ncol = 1)
  plt_act_paths
  # # Save to a PDF
  # actor_path_plotfile <- sprintf('%s_actor_pd_dist2peaks.pdf', self$UUID)
  # plot_dir <- getwd()
  # ggsave(filename = file.path(plot_dir, actor_path_plotfile), plt_act_paths, 
  #        width = 8, height = 8)
  
    
      
  
}
## 
## step 1: actors  1  2  3 
## step 13: actors  1  2  3 
## step 25: actors  1  2  3
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.